package com.xtx.service.impl;

import com.xtx.dto.AddShoppingCartDTO;
import com.xtx.entity.ShoppingCart;
import com.xtx.mapper.ShoppingMapper;
import com.xtx.service.ShoppingService;
import com.xtx.vo.ShoppingCartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ShoppingServiceImpl implements ShoppingService {

    @Autowired
    private ShoppingMapper shoppingMapper;

    @Override
    @Transactional
    public void addShoppingCart(AddShoppingCartDTO addShoppingCartDTO) {
        List<ShoppingCart> shoppingCarts = shoppingMapper.getShoppingCart(addShoppingCartDTO.getUserId());
        if (shoppingCarts.size()>0){
            for (ShoppingCart shopping : shoppingCarts){
                if (shopping.getAttrsText().equals(addShoppingCartDTO.getAttrsText())){
                    shopping.setCount(shopping.getCount()+1);
                    shoppingMapper.updataShoppingCart(shopping);
                    return;
                }
            }
        }
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .goodsId(addShoppingCartDTO.getId())
                .name(addShoppingCartDTO.getName())
                .picture(addShoppingCartDTO.getPicture())
                .price(addShoppingCartDTO.getPrice())
                .count(addShoppingCartDTO.getCount())
                .userId(addShoppingCartDTO.getUserId())
                .userName(addShoppingCartDTO.getUserName())
                .selected(addShoppingCartDTO.getSelected())
                .skuId(addShoppingCartDTO.getSkuId())
                .attrsText(addShoppingCartDTO.getAttrsText())
                .build();
        shoppingMapper.addShoppingCart(shoppingCart);
    }

    @Override
    public List<ShoppingCartVO> getShoppingCartList(Integer id) {
        List<ShoppingCartVO> shoppingCartList = shoppingMapper.getShoppingCartList(id);
        return shoppingCartList;
    }

    @Override
    @Transactional
    public void deleteShoppingCart(Integer id) {
        shoppingMapper.deleteShoppingCart(id);
    }

    @Override
    public void updataShoppingCart(ShoppingCart shoppingCart) {
        shoppingMapper.updataShoppingCart(shoppingCart);
    }

    @Override
    public void checkAll(Integer userId, ShoppingCart shoppingCart) {
        shoppingMapper.checkAll(userId,shoppingCart.getSelected());
    }
}
